﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace P_SetUp
{
    public partial class Form_LikeAddGrid : Form
    {
        public Form_LikeAddGrid()
        {
            InitializeComponent();
        }
        private bool IsBegin = false;
        private Mydb MyDB;
        private ArrayList SourceGridIdList;
        /// <summary>
        /// 目标GRID
        /// </summary>
        public string DesGridId;
        /// <summary>
        /// 效仿GRID
        /// </summary>
        private string SourceGridId;
        private void IniFrom()
        {
            IsBegin = false;
            if (DesGridId != null)
                this.label3.Text = DesGridId;
            else
                this.label3.Text = "未选择目标GRID";
            GetSourceGridIdList();
            IsBegin = true;
        }
        private void GetSourceGridIdList()
        {
            if (DesGridId != null)
            {
                MyDB = new Mydb();
                DataTable Temp = new DataTable();
                SourceGridIdList = new ArrayList();
                this.comboBox1.Items.Clear();
                string Sql = string.Format(@"Select AnyThing.ID
                    ,AnyThing.Name
                    From Sys_GridCols As AnyThing
                    Where AnyThing.ID <> '{0}' And AnyThing.Type='grid'
                    Order by AnyThing.Name", DesGridId);
                Temp = MyDB.GetDataTable(Sql);
                if (Temp != null)
                {
                    foreach (DataRow Row in Temp.Rows)
                    {
                        SourceGridIdList.Add(Row["ID"].ToString());
                        this.comboBox1.Items.Add(Row["Name"].ToString());
                    }
                }
                Temp.Dispose();
                if (SourceGridIdList.Count > 0)
                {
                    this.comboBox1.SelectedIndex = 0;
                    this.label4.Text = SourceGridIdList[0].ToString();
                }
                else
                {
                    this.label4.Text = "无效仿GRID";
                    this.comboBox1.SelectedIndex = -1;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (DesGridId != null)
                this.DialogResult = DialogResult.OK;
            else
            {
                MessageBox.Show("请选择目标GRID！");
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void Form_LikeAddGrid_Load(object sender, EventArgs e)
        {
            IniFrom();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsBegin)
            {
                int Index = (sender as ComboBox).SelectedIndex;
                if (Index > -1)
                {
                    this.label4.Text = SourceGridIdList[Index].ToString();
                    this.SourceGridId = SourceGridIdList[Index].ToString();
                }
            }
        }
    }
}
